#!/usr/local/bin/ruby
#
#  Copyright (c) 2009 Mu Dynamics, Inc. All rights reserved.


class Result < ActiveRecord::Base
  # belongs_to :run

  def self.latest
    return @latest
  end

  def self.test_id
    return @test_id
  end

  def self.test_name
    return @name
  end

  def self.run_id
    return @run_id
  end

  def self.find_by_test_id_and_run_id(test_id, run_id, latest=false, find_params=nil)
    @test_id = test_id
    @name = nil
    @run_id = run_id
    @latest = latest

    direction = "ASC"
    order_by = 'test_id'
    if !find_params[:order].nil?
      parms = find_params[:order].split(" ")
      order_by = parms[0]
      direction = parms[1]
    end

    if latest == false
       stmt = "select * from results where test_id='#{test_id}' and run_id='#{run_id}' ORDER BY #{order_by} #{direction}"
    else
       stmt = "SELECT p.* FROM results AS p
             JOIN (
             SELECT name, MAX(created_at) AS created_at
             FROM results
             WHERE test_id='#{test_id}' and run_id='#{run_id}'
             GROUP BY name
             ORDER BY #{order_by} #{direction}
             ) AS pm ON p.name = pm.name AND p.created_at = pm.created_at;"
    end
    return find_by_sql(stmt)
  end

  def self.find_by_test_id(test_id, latest=false, find_params=nil)
    @test_id = test_id
    @name = nil
    @run_id = nil
    @latest = latest

    direction = "ASC"
    order_by = 'test_id'
    if !find_params[:order].nil?
      parms = find_params[:order].split(" ")
      order_by = parms[0]
      direction = parms[1]
    end
    
    if latest == false
       stmt = "select * from results where test_id='#{test_id}' ORDER BY #{order_by} #{direction}"
    else
       stmt = "SELECT p.* FROM results AS p
             JOIN (
             SELECT name, MAX(created_at) AS created_at
             FROM results
             WHERE test_id='#{test_id}'
             GROUP BY name
             ORDER BY #{order_by} #{direction}
             ) AS pm ON p.name = pm.name AND p.created_at = pm.created_at;"
    end
    return find_by_sql(stmt)
  end

  def self.find_by_run_id(run_id, latest=false, find_params=nil)
    @test_id = nil
    @name = nil
    @run_id = run_id
    @latest = latest

    direction = "ASC"
    order_by = 'test_id'
    if !find_params[:order].nil?
      parms = find_params[:order].split(" ")
      order_by = parms[0]
      direction = parms[1]
    end

    if latest == false
        stmt = "select * from results where run_id='#{run_id}' ORDER BY #{order_by} #{direction}"
    else
         stmt = "SELECT p.* FROM results AS p
             JOIN (
             SELECT name, MAX(created_at) AS created_at
             FROM results
             WHERE run_id='#{run_id}'
             GROUP BY name
             ORDER BY #{order_by} #{direction}
             ) AS pm ON p.name = pm.name AND p.created_at = pm.created_at;"
    end
    return find_by_sql(stmt)
  end

  def self.find_by_name(name, latest=false, find_params=nil)
    @test_id = nil
    @name = name
    @run_id = nil
    @latest = latest

    direction = "ASC"
    order_by = 'name'
    if !find_params[:order].nil?
      parms = find_params[:order].split(" ")
      order_by = parms[0]
      direction = parms[1]
    end

    if latest == false
        stmt = "select * from results where name='#{name}' ORDER BY #{order_by} #{direction}"
    else
         stmt = "SELECT p.* FROM results AS p
             JOIN (
             SELECT name, MAX(created_at) AS created_at
             FROM results
             WHERE name='#{name}'
             GROUP BY name
             ORDER BY #{order_by} #{direction}
             ) AS pm ON p.name = pm.name AND p.created_at = pm.created_at;"
    end
    return find_by_sql(stmt)
  end

  def self.find_by_name_and_run_id(name, run_id, latest=false, find_params=nil)
    @test_id = nil
    @name = name
    @run_id = run_id
    @latest = latest
    
    direction = "ASC"
    order_by = 'name'
    if !find_params[:order].nil?
      parms = find_params[:order].split(" ")
      order_by = parms[0]
      direction = parms[1]
    end
    
    if latest == false
       stmt = "select * from results where name='#{name}' and run_id='#{run_id}' ORDER BY #{order_by} #{direction}"
    else
       stmt = "SELECT p.* FROM results AS p
             JOIN (
             SELECT name, MAX(created_at) AS created_at
             FROM results
             WHERE name='#{name}' and run_id='#{run_id}'
             GROUP BY name
             ORDER BY #{order_by} #{direction}
             ) AS pm ON p.name = pm.name AND p.created_at = pm.created_at;"
    end
    return find_by_sql(stmt)
  end

end
